import { createApp } from 'vue';
import App from './App.vue';
import router from './router';

const app = createApp(App);

app.use(router);

import 'element-plus/dist/index.css';

import ElementPlus from 'element-plus';

import locale from 'element-plus/es/locale/lang/zh-CN';

import 'virtual:svg-icons-register';

import $ from 'jquery';

import { ElMessage } from 'element-plus';

// 后端接口地址
let baseUrl = 'http://localhost:8080/YiLiaoJianKang-Api';
app.config.globalProperties.$baseUrl = baseUrl;

let minioUrl = 'http://localhost:9000/yiliaojiankang';
app.config.globalProperties.$minioUrl = minioUrl;

// Ajax请求函数
app.config.globalProperties.$http = function (url: string, method: string, data: JSON, async: boolean, fun: Function) {
    $.ajax({
        url: baseUrl + url,
        type: method,
        dataType: 'json',
        contentType: 'application/json',
        traditional: true,
        xhrFields: {
            withCredentials: true
        },
        headers: {
            token: localStorage.getItem('token')
        },
        async: async,
        data: JSON.stringify(data),
        success: function (resp: any) {
            if (resp.code == 200) {
                fun(resp);
            } else {
                ElMessage.error({
                    message: resp.msg,
                    duration: 1200
                });
            }
        },
        error: function (e: any) {
            if (e.status == undefined) {
                ElMessage.error({
                    message: '前端页面错误',
                    duration: 1200
                })
            } else {
                let status = e.status;
                // 没有登录体检系统
                if (status == 401) {
                    router.push({
                        name: 'FrontIndex'
                    });
                    if (url.startsWith('/front/')) {

                    } else {
                        router.push({
                            name: 'MisLogin'
                        })
                    }
                } else {
                    //  后端没有运行正确
                    if (!e.hasOwnProperty('responseText')) {
                        ElMessage.error({
                            message: '后端运行有误',
                            duration: 1200
                        });
                    } else {
                        ElMessage.error({
                            message: e.responseText,
                            duration: 1200
                        });
                    }
                }
            }
        }
    })
};

// 鉴权函数
app.config.globalProperties.isAuth = function (permission:string[]) {
    // console.log(permission);
    // const permissions:string | null = localStorage.getItem('permissions');
    const permissions = permission;
    if (permissions) {
        let flag = false;
        for (let one of permission) {
            if (permissions.includes(one)) {
                flag = true;
                break;
            }
        }
        // console.log(permissions);
        return flag;
    } else {
        return false;
    }
}

app.use(ElementPlus, {
    locale
});

import * as ElementPlusIconsVue from '@element-plus/icons-vue'
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component);
}

app.mount('#app');


